package top.jolyoulu.streaming

import org.apache.spark.SparkConf
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.receiver.Receiver
import org.apache.spark.streaming.{Seconds, StreamingContext}

import scala.util.Random

/**
 * @Author: JolyouLu
 * @Date: 2024/5/19 15:07
 * @Description
 */
object Spark01_SparkStreaming_State_Join {
  def main(args: Array[String]): Unit = {
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkStreaming")
    val ssc = new StreamingContext(sparkConf, Seconds(5))

    val lines9999: ReceiverInputDStream[String] = ssc.socketTextStream("192.168.88.100", 9999)
    val lines8888: ReceiverInputDStream[String] = ssc.socketTextStream("192.168.88.100", 8888)
    //转换结构
    val map9999: DStream[(String, Int)] = lines9999.map((_, 1))
    val map8888: DStream[(String, Int)] = lines8888.map((_, 1))
    //合并数据
    val joinDS: DStream[(String, (Int, Int))] = map9999.join(map8888)
    //打印
    joinDS.print()

    ssc.start()
    ssc.awaitTermination()
  }
}
